import { defineStore } from 'pinia'
import { 
  getArticleList, 
  createArticle, 
  updateArticle, 
  updateArticleStatus, 
  deleteArticle 
} from '../api/article'

export const useArticleStore = defineStore('article', {
  state: () => ({
    articleList: [],
    total: 0,
    loading: false
  }),
  
  actions: {
    async fetchArticleList(params) {
      this.loading = true
      try {
        const response = await getArticleList(params)
        if (response.data && response.data.data) {
          this.articleList = response.data.data.records
          this.total = response.data.data.total
        }
      } catch (error) {
        console.error('Fetch article list error:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    async createArticle(articleData) {
      try {
        const response = await createArticle(articleData)
        return response
      } catch (error) {
        console.error('Create article error:', error)
        throw error
      }
    },
    
    async updateArticle(id, articleData) {
      try {
        const response = await updateArticle(id, articleData)
        return response
      } catch (error) {
        console.error('Update article error:', error)
        throw error
      }
    },
    
    async updateArticleStatus(id, status) {
      try {
        const response = await updateArticleStatus(id, status)
        return response
      } catch (error) {
        console.error('Update article status error:', error)
        throw error
      }
    },
    
    async deleteArticle(id) {
      try {
        const response = await deleteArticle(id)
        return response
      } catch (error) {
        console.error('Delete article error:', error)
        throw error
      }
    }
  }
}) 